package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //加request是想登陆成功后，把Employee存入session中一份，想要用到的时候可以用request取出来
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//②. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> emp = new LambdaQueryWrapper<>();
        //对比用户名是否相同
        emp.eq(Employee::getUsername, employee.getUsername());
        //对比密码是否相同
        emp.eq(Employee::getPassword, password);
        Employee one = employeeService.getOne(emp);
//③. 如果没有查询到或者账号密码错误为空, 则返回登录失败结果
        if (one == null) {
            return R.error("登陆失败,请检查账号或密码是否正确");
        }
   /* //④. 密码比对，如果不一致, 则返回登录失败结果
    if (!one.getPassword().equals(password)){
        return  R.error("登陆失败");
    }*/
        //⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus() == 0) {
            return R.error("此账号已禁用");
        }
        //⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        //清理Session中保存的当前登录员工的id
        session.invalidate();
        return R.success("退出成功");
    }


    //新增员工，由于新增员工的路径没有子路径，所以此处的PostMapping不需要添加路径
    @PostMapping//由于是要接受JSON数据，所以此处的参数需要加RequestBody
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工：{}", employee.toString());
        String s = DigestUtils.md5DigestAsHex("111111".getBytes());
        employee.setPassword(s);
/*employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
//获取当前用户ID
  Long employee1 = (Long) request.getSession().getAttribute("employee");
    employee.setCreateUser(employee1);
    employee.setUpdateUser(employee1);*/
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    /**
     * 员工信息分页查询
     *
     * @param page     当前查询页码
     * @param pageSize 每页展示记录数
     * @param name     员工姓名 - 可选参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
      /*  Long employee1 = (Long) request.getSession().getAttribute("employee");
 employee.setUpdateTime(LocalDateTime.now());
 employee.setUpdateUser(employee1);*/
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> ById(@PathVariable Long id) {
        log.info("根据Id查询");
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
